/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.refunite.ws;



import java.io.OutputStream;
import java.io.PrintStream;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.refunite.exception.TechnicalException;

/**
 *
 * @author I040022
 */
public class WebServiceProvider {
	    private final static String METHOD_SAVE = "saveRegistrationForm";
	    private final static String METHOD_GET = "getLatestRegistrationForm";
	    private final static String NAMESPACE = "http://webservice.mobile.refunite.org/";
	    private final static String SOAP_ACTION_SAVE = NAMESPACE + METHOD_SAVE;
	    private final static String SOAP_ACTION_GET = NAMESPACE + METHOD_GET;
	    private final static String WSDL_URL = "/RefugeesUnitedWebService/RegistrationFormWS?wsdl";

    public static String callSaveRegistrationFormWS(String formContent, String serverURL) throws TechnicalException{
        SoapObject request = new SoapObject(NAMESPACE, METHOD_SAVE);
        //parameters to be passed to the method (parameter name, value)
        request.addProperty("formContent", formContent);

        /*
        Element[] header = new Element[1];
        header[0] = new Element().createElement(NAMESPACE, "AuthHeader");
        Element username = new Element().createElement(NAMESPACE, "Username");
        username.addChild(Node.TEXT, "test");
        header[0].addChild(Node.ELEMENT, username);
        Element pass = new Element().createElement(NAMESPACE, "Password");
        pass.addChild(Node.TEXT, "test");
        header[0].addChild(Node.ELEMENT, pass);
         */
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
    //    ServerConfigData serverConfigData = PersistenceStoreProvider.getServerConfiguration();
    //    String serverURL = serverConfigData.getServerAddress();
        //String serverURL = "http://127.0.0.1:8080";
        String retValue = "false";

        try {
            /*
            String path = System.getProperty("fileconn.dir.private");
            System.out.println("Private folder:"+path+"log.txt");
            FileConnection fileConnection = (FileConnection) Connector.open(path+"log.txt",Connector.READ_WRITE);
            if(!fileConnection.exists()){
                fileConnection.create();
            }
            OutputStream stream = fileConnection.openOutputStream(fileConnection.fileSize());
            PrintStream printStream = new PrintStream(stream);
            printStream.println(request.toString());
            printStream.println(formContent);
            printStream.close();
            stream.close();
            fileConnection.close();
            */
            HttpTransportSE httpTransport = new HttpTransportSE(serverURL + WSDL_URL);
            httpTransport.debug = true;
   
            httpTransport.call(SOAP_ACTION_SAVE, envelope);
            retValue = envelope.getResponse().toString();
        } catch (SoapFault sf) {
            sf.printStackTrace();
            throw new TechnicalException(sf);
        } catch (Exception e) {
            e.printStackTrace();
            throw new TechnicalException(e);
        }
        return retValue;

    }
    public static String callGetCurrentRegistrationFormWS(String serverURL) throws TechnicalException{
        SoapObject request = new SoapObject(NAMESPACE, METHOD_GET);

        /*
        Element[] header = new Element[1];
        header[0] = new Element().createElement(NAMESPACE, "AuthHeader");
        Element username = new Element().createElement(NAMESPACE, "Username");
        username.addChild(Node.TEXT, "test");
        header[0].addChild(Node.ELEMENT, username);
        Element pass = new Element().createElement(NAMESPACE, "Password");
        pass.addChild(Node.TEXT, "test");
        header[0].addChild(Node.ELEMENT, pass);
         */
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
                String retValue = "false";
        try {
            HttpTransportSE httpTransport = new HttpTransportSE(serverURL + WSDL_URL);
            httpTransport.debug = true;
            httpTransport.call(SOAP_ACTION_GET, envelope);
            retValue = envelope.getResponse().toString();
        } catch (SoapFault sf) {
            throw new TechnicalException(sf);
        } catch (Exception e) {
            throw new TechnicalException(e);
        }
        return retValue;
    }
}
